from django.db import models


# Create your models here.
class SysUser(models.Model):
    id = models.AutoField(primary_key=True)
    username = models.CharField(max_length=100, unique=True, verbose_name="用户名")
    password = models.CharField(max_length=100, verbose_name="密码")
    avatar = models.CharField(max_length=255, null=True, verbose_name="头像")
    email = models.CharField(max_length=100, null=True, verbose_name="邮箱")
    phonenumber = models.CharField(max_length=11, null=True, verbose_name="手机号")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="修改时间")
    login_time = models.DateTimeField(null=True, verbose_name="最后登录时间")
    status = models.IntegerField(null=True, verbose_name="账号状态")  # 0正常，1停用
    remark = models.CharField(max_length=500, null=True, verbose_name="备注")

    def __str__(self):
        return self.username

    class Meta:
        db_table = "sys_user"
        
class SystemLog(models.Model):
    level = models.CharField(max_length=10, verbose_name="日志级别")
    message = models.TextField(verbose_name="日志信息")
    method = models.CharField(max_length=10, null=True, verbose_name="请求方法")
    path = models.CharField(max_length=255, null=True, verbose_name="请求路径")
    status_code = models.IntegerField(null=True, verbose_name="响应状态码")
    duration = models.FloatField(null=True, verbose_name="请求耗时（秒）")
    ip_address = models.GenericIPAddressField(null=True, verbose_name="客户端IP地址")
    user_agent = models.CharField(max_length=255, null=True, verbose_name="用户代理")
    username = models.CharField(max_length=100, null=True, verbose_name="用户名")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    
    class Meta:
        db_table = "sys_log"
        verbose_name = "系统日志"
        verbose_name_plural = verbose_name # 复数形式
        ordering = ['-create_time'] # 默认按创建时间降序排列
        indexes = [
        models.Index(fields=["-create_time"]), #加索引能让查询和删除都更快
        models.Index(fields=["level"]),
        models.Index(fields=["username"]),
    ]
